package com.zxgj.datadirect.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zxgj.common.annotation.Anonymous;
import com.zxgj.common.annotation.Log;
import com.zxgj.common.core.controller.BaseController;
import com.zxgj.common.core.domain.AjaxResult;
import com.zxgj.common.enums.BusinessType;
import com.zxgj.datadirect.entity.DataDirectOrg;
import com.zxgj.datadirect.service.DataDirectOrgService;
import com.zxgj.common.utils.poi.ExcelUtil;
import com.zxgj.common.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 机构信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-04
 */
@RestController
@RequestMapping("/datadirect/org")
public class DataDirectOrgController extends BaseController
{
    @Autowired
    private DataDirectOrgService dataDirectOrgService;

    /**
     * 查询机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataDirectOrg dataDirectOrg)
    {
        startPage();
        List<DataDirectOrg> list = dataDirectOrgService.getOrgList(dataDirectOrg);
        return getDataTable(list);
    }

    /**
     * 分页查询机构信息列表
     */
    @Anonymous
    @GetMapping("/page")
    public TableDataInfo page(DataDirectOrg dataDirectOrg, 
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize)
    {
        Page<DataDirectOrg> page = new Page<>(pageNum, pageSize);
        Page<DataDirectOrg> result = dataDirectOrgService.getOrgPage(page, dataDirectOrg);
        
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(result.getRecords());
        tableDataInfo.setTotal(result.getTotal());
        
        return tableDataInfo;
    }

    /**
     * 导出机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:export')")
    @Log(title = "机构信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataDirectOrg dataDirectOrg)
    {
        List<DataDirectOrg> list = dataDirectOrgService.getOrgList(dataDirectOrg);
        ExcelUtil<DataDirectOrg> util = new ExcelUtil<DataDirectOrg>(DataDirectOrg.class);
        util.exportExcel(response, list, "机构信息数据");
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dataDirectOrgService.getById(id));
    }

    /**
     * 根据机构编码获取机构信息
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:query')")
    @GetMapping(value = "/code/{orgCode}")
    public AjaxResult getByOrgCode(@PathVariable("orgCode") String orgCode)
    {
        return success(dataDirectOrgService.getByOrgCode(orgCode));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:add')")
    @Log(title = "机构信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataDirectOrg dataDirectOrg)
    {
        return toAjax(dataDirectOrgService.addOrg(dataDirectOrg));
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:edit')")
    @Log(title = "机构信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataDirectOrg dataDirectOrg)
    {
        return toAjax(dataDirectOrgService.updateOrg(dataDirectOrg));
    }

    /**
     * 删除机构信息
     */
    @PreAuthorize("@ss.hasPermi('datadirect:org:remove')")
    @Log(title = "机构信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dataDirectOrgService.deleteOrgBatch(ids));
    }

    /**
     * 获取机构统计信息
     */
    @Anonymous
    @GetMapping("/stats")
    public AjaxResult getStats()
    {
        List<DataDirectOrg> stats = dataDirectOrgService.getOrgStats();
        return success(stats);
    }

    /**
     * 获取上级机构选项
     */
    @Anonymous
    @GetMapping("/parent-options")
    public AjaxResult getParentOptions()
    {
        List<DataDirectOrg> options = dataDirectOrgService.getParentOrgOptions();
        return success(options);
    }
}
